from rest_framework.generics import ListAPIView
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAdminUser

from meiduo_admin.serializers.statistical import GoodsVisitCountSerialzier
from users.models import User
from datetime import date, timedelta
from goods.models import GoodsVisitCount
from orders.models import OrderInfo


class UserCount(APIView):
    """
        用户总数统计
    """
    # 指定用户权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 1、查询所有用户总量
        count = User.objects.all().count()
        # 2、获取当前日期
        data_date = date.today()
        # 3、返回用户总量和当前日期
        return Response({
            "count": count,
            "date": data_date
        })


class UserDayCount(APIView):
    """
        日增用户统计
    """
    # 指定用户权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 1、获取当前日期
        data_date = date.today()
        # 2、查询当天注册用户数量
        count = User.objects.filter(date_joined__gte=data_date).count()
        # 3、返回用户总量和当前日期
        return Response({
            "count": count,
            "date": data_date
        })


class UserDayActiveCount(APIView):
    """
        日活用户统计
    """
    # 指定用户权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 1、获取当前日期
        data_date = date.today()
        # 2、查询当天登录用户数量
        count = User.objects.filter(last_login__gte=data_date).count()
        # 3、返回用户总量和当前日期
        return Response({
            "count": count,
            "date": data_date
        })


class UserDayOrderCount(APIView):
    """
        下单用户统计
    """
    # 指定用户权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 1、获取当前日期
        data_date = date.today()
        # 2、查询当天下订单用户数量
        count= len(set(User.objects.filter(orders__create_time__gte=data_date)))
        # count = User.objects.filter(orders__create_time__gte=data_date).count()
        # 3、返回用户总量和当前日期
        return Response({
            "count": count,
            "date": data_date
        })


class UserMonthCount(APIView):
    """
        月新增用户统计
    """
    # 指定用户权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 1、获取当前日期
        data_date = date.today()
        # 2、获取一个月之前的日期
        begin_date = data_date - timedelta(29)
        # 3、遍历一个月中的每天数据
        data_list = []
        for i in range(30):
            # 确定当天日期  4-28
            index_date = begin_date + timedelta(days=i)
            # 确定第二天日期 4-29
            next_date = begin_date + timedelta(days=i + 1)
            count = User.objects.filter(date_joined__gte=index_date, date_joined__lt=next_date).count()
            data_list.append({
                "count": count,
                "date": index_date
            })
        # 3、返回用户总量和当前日期
        return Response(data_list)


class GoodsVisitCountView(APIView):
    """
        分类数量统计
    """
    # 指定用户权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 1、获取当前日期
        data_date = date.today()
        # 2、查询分类数量表
        data = GoodsVisitCount.objects.filter(date=date.today())

        ser = GoodsVisitCountSerialzier(data, many=True)
        return Response(ser.data)


